"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var WechatyApiEventType;
(function (WechatyApiEventType) {
    WechatyApiEventType[WechatyApiEventType["login"] = 0] = "login";
    WechatyApiEventType[WechatyApiEventType["logout"] = 1] = "logout";
    WechatyApiEventType[WechatyApiEventType["qrcode"] = 2] = "qrcode";
    WechatyApiEventType[WechatyApiEventType["error"] = 3] = "error";
    WechatyApiEventType[WechatyApiEventType["friend-request"] = 4] = "friend-request";
    WechatyApiEventType[WechatyApiEventType["friend-be-confirm"] = 5] = "friend-be-confirm";
    WechatyApiEventType[WechatyApiEventType["friend-do-confirm"] = 6] = "friend-do-confirm";
    WechatyApiEventType[WechatyApiEventType["room-join"] = 7] = "room-join";
    WechatyApiEventType[WechatyApiEventType["room-leave"] = 8] = "room-leave";
    WechatyApiEventType[WechatyApiEventType["room-removed"] = 9] = "room-removed";
    WechatyApiEventType[WechatyApiEventType["room-topic-change"] = 10] = "room-topic-change";
    WechatyApiEventType[WechatyApiEventType["message"] = 11] = "message";
})(WechatyApiEventType = exports.WechatyApiEventType || (exports.WechatyApiEventType = {}));
// const WechatyApiEventTypes = [
//   'login',
//   'logout',
//   'qrcode',
//   'error',
//   'friend-request',
//   'friend-be-confirm',
//   'friend-do-confirm',
//   'room-join',
//   'room-leave',
//   'room-removed',
//   'room-topic-change',
//   'message'
// ]
class WechatEvent {
    constructor(app) {
        this.app = app;
        this.handlers = {};
    }
    route(controller) {
        return this.app.post(`/${this.app.config.wechatApi.apiEventPrefix}`, controller);
    }
    handlerEntrance(ctx, body) {
        this.app.logger.debug(`[egg-wechat-api] handlerEntrance event body`, body);
        if (!body.type) {
            throw new Error('wrong event body');
        }
        let uuid = body.wx;
        let eventType = body.type;
        let handler = this.handlers[uuid] && this.handlers[uuid][eventType];
        if (handler) {
            return handler(body, ctx);
        }
        else {
            return ctx.body = {
                err_code: 0,
                err_msg: 'success'
            };
        }
    }
    on(uuid, eventType, handler) {
        if (!handler || Object.prototype.toString.apply(handler) !== '[object Function]') {
            throw new Error('wrong handler type');
        }
        this.app.logger.debug(`[egg-wechat-api] event on`, eventType);
        if (!this.handlers[uuid]) {
            this.handlers[uuid] = {};
        }
        this.handlers[uuid][eventType] = handler;
    }
    once(uuid, eventType, handler) {
        if (!handler || Object.prototype.toString.apply(handler) !== '[object Function]') {
            throw new Error('wrong handler type');
        }
        this.app.logger.debug(`[egg-wechat-api] event once`, eventType);
        if (!this.handlers[uuid]) {
            this.handlers[uuid] = {};
        }
        this.handlers[uuid][eventType] = (event, ctx) => {
            handler(event, ctx);
            delete this.handlers[uuid][eventType];
        };
    }
    off(uuid, eventType) {
        this.app.logger.debug(`[egg-wechat-api] event off`, eventType);
        delete this.handlers[uuid][eventType];
    }
}
exports.default = WechatEvent;
